// Description: Unit Test for testing Product_BeforeInsertUpdate Trigger
// Author(s): S.Gare & R.Clark
// Copyright: BrightGen Ltd (http://www.brightgen.com)
// Date: 12/03/2008
public class ProductIntegrationTests {

	public static TestMethod void testProductInsert() {
	
		// Unit test product
		Product2 product = new Product2(Name='Unit Test Product', IsActive=true);
		insert product;
		
		ID insertedProductId = [SELECT p.Id 
								FROM PRODUCT2 p 
								WHERE p.Name='Unit Test Product'][0].ID;
								
		Integer priceBookEntryCount = [SELECT count() 
										FROM PriceBookEntry pbe 
										WHERE Product2ID = :insertedProductId];
		
		// Find the number of active currencies first to compare that there is 
		// a price book entry for each one.		
		Integer numCurrencies = [SELECT count()
								   FROM CurrencyType ct
								  WHERE ct.IsActive = True];
								  						
		System.assertEquals(numCurrencies, priceBookEntryCount);
		
		//Tear Down
		product = [SELECT p.Id FROM PRODUCT2 p WHERE p.ID=:insertedProductId];
		
		delete product;
		
	}

	public static TestMethod void testProductInsertUpdate()  {
		
		// Create Product
		Product2 productNew = new Product2(Name='Unit Test ProductInsertUpdate', SAPProductCode__c = 'BGTESTPROD001', ProductType__c='SAPProduct');
		insert productNew;
		
		Product2 productCreated = [SELECT p.IsActive,p.ID,
										  p.Name,
										  p.ProductCode,
										  p.SAPProductCode__c,
										  p.ProductType__c
								FROM PRODUCT2 p 
								WHERE p.Name='Unit Test ProductInsertUpdate'];
		 
		// Check Trigger Result
		System.assertEquals(True, productCreated.IsActive);
		
		System.assertEquals(True, (productCreated.ProductCode == productCreated.SAPProductCode__c)); 

		// Tear Down
		delete productCreated;

	}

}